/*
 * Professional Android, 4th Edition
 * Reto Meier and Ian Lake
 * Copyright 2018 John Wiley Wiley & Sons, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.professionalandroid.apps.earthquake;

import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
import android.app.Application;
import android.location.Location;
/**
 * 地震模型实体对象
 */
public class Earthquake {
  /**
   * key id
   */
  private final  String mId;
  /**
   * 地震发生日期
   */
  private  Date mDate;
  /**
   * 地震详情
   */
  private String mDetails;
  /**
   * 位置坐标
   */
  private Location mLocation;
  /**
   * 震级
   */
  private double mMagnitude;
  /**
   * 链接
   */
  private  String mLink;
  /**
   *  返回id
   *  @return 地震实体对象的id
   */
  public String getId() { return mId; }
  public Date getDate() { return mDate; }
  public String getDetails() { return mDetails; }
  public Location getLocation() { return mLocation; }
  public double getMagnitude() { return mMagnitude; }
  public String getLink() { return mLink; }

  /**
   * 地震模型实体对象
   * @param id key
   * @param date  发生日期
   * @param details  详情
   *   @param location 位置 坐标
   *   @param magnitude 震级
   *   @param link 链接
   *   @return 地震实体对象
   * @exception NullPointerException 空指针异常 if the given pattern or locale is null
   * @exception IllegalArgumentException 非法参数异常  if the given pattern is invalid
   */
  public Earthquake(String id, Date date, String details,
                    Location location,
                    double magnitude, String link) {
    mId = id;
    mDate = date;
    mDetails = details;
    mLocation = location;
    mMagnitude = magnitude;
    mLink = link;
  }

  @Override
  public String toString() {

//    TODO: test  https://blog.csdn.net/qq_43353521/article/details/109954189
//    SimpleDateFormat F1 =new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
//    SimpleDateFormat F2 =new SimpleDateFormat("yy/MM/dd HH:mm");
//    SimpleDateFormat F3 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    SimpleDateFormat F4 =new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E");
//    SimpleDateFormat F5 =new SimpleDateFormat("一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
//    //解析方法parse
//    Date date1 = F1.parse("2018年12月08日 08时08分08秒");
//    Date date2 = F2.parse("2018/12/08 09:28");
//    Date date3 = F3.parse("2018-12-08 12:15:56");

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E", Locale.CHINA);
    String dateString = sdf.format(mDate);
    mLink = mLink == null ? "无数据" : mLink;

    String mLocation2=" ";
    if (mLocation == null) mLocation2 = "暂未获取";
    else mLocation2 = mLocation.getLongitude()+","+ mLocation.getLatitude();
    return dateString + "\n震级[" + mMagnitude + "] 详情[" + mDetails+ "]\n位置[" + mLocation2+ "] 访问：" + mLink;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj instanceof Earthquake)
      return (((Earthquake)obj).getId().contentEquals(mId));
    else
      return false;
  }
}